﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Collections;

namespace Server
{
    public partial class MainForm : Form
    {
        #region -  VARIABLES  -
        public string ipAddress = null;
        private ServerProgram server = null;
        private SendFile sendFile = null;
        private MyMessage message = null;
        private DataPackage package = null;
        private AttendenceCheckForm attendenceCheckForm = null;
        private SaveFileToDirectory saveFileToDirectory = null;
        private int id = 0;
        private bool status = false;
        #endregion

        #region -  CONTRUCTOR  -
        public MainForm()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;

            this.server = new ServerProgram();
            this.server.OnUnCheckUSBDetect += new UnCheckUSBDetectEventhHandler(server_OnUnCheckUSBDetect);
            this.server.OnClientExit += new ClientExitEventHandler(server_OnClientExit);
            this.server.OnNewClient += new NewClientEventHandler(server_OnNewClient);
        }

        

        #endregion

        #region -  UN CHECK USB DETECT  -
        void server_OnUnCheckUSBDetect(object sender, UnCheckUSBDetectEventArgs e)
        {
            this.enableUSBDetectToolStripMenuItem.Checked = false;
        }
        #endregion

        #region -  SERVER EVENTS  -
        void server_OnNewClient(object sender, NewClientEventArgs e)
        {
            foreach (ListViewItem item in this.listView1.Items)
            {
                if (item.Text.Equals(e.IpAddress))
                {
                    item.ImageIndex = 0;
                    this.status = true;
                }
            }
            if (!this.status)
            {
                this.listView1.Items.Add(e.IpAddress, 0);
            }
            this.listView1.Refresh();
            this.status = false;
        }

        void server_OnClientExit(object sender, ClientExitEventArgs e)
        {
            try
            {
                foreach (ListViewItem item in this.listView1.Items)
                {
                    if (item.Text.Equals(e.IpAddress))
                        item.ImageIndex = 7;
                }
                this.listView1.Refresh();
            }
            catch
            { }
        }
        #endregion

        #region -  SEND  -
        private void messageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClientProgram c = this.server.FindClient(this.ipAddress);
            c.SetMessageBox(this.ipAddress);
            c.ShowMessageBox1();
        }

        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.sendFile = new SendFile();
            this.sendFile.OnGetFilePath += new GetFilePathEventHandler(sendFile_OnGetFilePath);
            this.sendFile.Show();
        }

        void sendFile_OnGetFilePath(object sender, FileInfoEventArgs e)
        {
            ClientProgram c = this.server.FindClient(this.ipAddress);
            c.SetFilePath(e.FilePath);
            c.sendQueue.Enqueue(new DataPackage(Command.Data, Path.GetFileName(e.FilePath), e.Length));
        }
        #endregion

        #region -  GET IP ADDRESS  -
        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                this.ipAddress = this.listView1.GetItemAt(e.X, e.Y).Text;
                this.id = this.listView1.GetItemAt(e.X, e.Y).Index;
            }
            catch
            { }
        }
        #endregion

        #region -  MAIN FORM CLOSED EVENT  -
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                this.server.StopServer();
                StringBuilder sb = new StringBuilder();
                foreach (ListViewItem item in this.listView1.Items)
                {
                    sb.Append(item.Text + "|");
                }
                AccessConfigFile.WriteFile(sb.ToString(), "ListClients.dat");
            }
            catch
            { }
        }
        #endregion

        #region -  TURN OFF COMPUTER  -
        private void shToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClientProgram c = this.server.FindClient(this.ipAddress);
            c.sendQueue.Enqueue(new DataPackage(Command.Cmd, null, 1));
            c.sendQueue.Enqueue(CommandType.Shutdown);
            this.ChangeIcon(this.listView1.Items[this.id], 6);
            this.listView1.Refresh();
        }

        private void standByToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClientProgram c = this.server.FindClient(this.ipAddress);
            c.sendQueue.Enqueue(new DataPackage(Command.Cmd, null, 1));
            c.sendQueue.Enqueue(CommandType.Standby);
            this.ChangeIcon(this.listView1.Items[this.id], 1);
            this.listView1.Refresh();
        }

        private void logOffToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClientProgram c = this.server.FindClient(this.ipAddress);
            c.sendQueue.Enqueue(new DataPackage(Command.Cmd, null, 1));
            c.sendQueue.Enqueue(CommandType.LogOff);
            this.ChangeIcon(this.listView1.Items[this.id], 4);
            this.listView1.Refresh();
        }

        private void restartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClientProgram c = this.server.FindClient(this.ipAddress);
            c.sendQueue.Enqueue(new DataPackage(Command.Cmd, null, 1));
            c.sendQueue.Enqueue(CommandType.Restart);
            this.ChangeIcon(this.listView1.Items[this.id], 5);
            this.listView1.Refresh();
        }

        private void hibernateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClientProgram c = this.server.FindClient(this.ipAddress);
            c.sendQueue.Enqueue(new DataPackage(Command.Cmd, null, 1));
            c.sendQueue.Enqueue(CommandType.Hibernate);
            this.ChangeIcon(this.listView1.Items[this.id], 2);
            this.listView1.Refresh();
        }

        private void lockComputerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClientProgram c = this.server.FindClient(this.ipAddress);
            c.sendQueue.Enqueue(new DataPackage(Command.Cmd, null, 1));
            c.sendQueue.Enqueue(CommandType.LockComputer);
            this.ChangeIcon(this.listView1.Items[this.id], 3);
            this.listView1.Refresh();
        }
        #endregion

        #region -  SEND MESSAGE TO ALL CLIENTS  -
        private void sendMessageToAllClientsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.message = new MyMessage();
            this.message.OnSendMessage += new SendMessageEventHandler(message_OnSendMessage);
            this.message.Show();
        }

        void message_OnSendMessage(object sender, MessageEventArgs e)
        {
            foreach (ClientProgram c in this.server.listClients)
            {
                c.sendQueue.Enqueue(new DataPackage(Command.Text, null, e.Message.Length));
                c.sendQueue.Enqueue(e.Message);
            }
        }
        #endregion

        #region -  SEND FILE TO ALL CLIENTS  -
        private void sendFileToAllCientsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.sendFile = new SendFile();
            this.sendFile.OnGetFilePath += new GetFilePathEventHandler(sendFile_OnGetFilePath1);
            this.sendFile.Show();
        }

        void sendFile_OnGetFilePath1(object sender, FileInfoEventArgs e)
        {
            foreach (ClientProgram c in this.server.listClients)
            {
                c.SetFilePath(e.FilePath);
                c.sendQueue.Enqueue(new DataPackage(Command.Data, Path.GetFileName(e.FilePath), e.Length));
            }
        }
        #endregion

        #region -  TURN OFF ALL CLIENTS  -
        private void shutdownAllClientsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (ClientProgram c in this.server.listClients)
            {
                c.sendQueue.Enqueue(new DataPackage(Command.Cmd, null, 1));
                c.sendQueue.Enqueue(CommandType.Shutdown);
            }
            foreach (ListViewItem i in this.listView1.Items)
            {
                this.ChangeIcon(i, 6);
            }
            this.listView1.Refresh();
        }

        private void standByAllClientsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (ClientProgram c in this.server.listClients)
            {
                c.sendQueue.Enqueue(new DataPackage(Command.Cmd, null, 1));
                c.sendQueue.Enqueue(CommandType.Standby);
            }
            foreach (ListViewItem i in this.listView1.Items)
            {
                this.ChangeIcon(i, 1);
            }
            this.listView1.Refresh();
        }

        private void logOffAllClientsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (ClientProgram c in this.server.listClients)
            {
                c.sendQueue.Enqueue(new DataPackage(Command.Cmd, null, 1));
                c.sendQueue.Enqueue(CommandType.LogOff);
            }
            foreach (ListViewItem i in this.listView1.Items)
            {
                this.ChangeIcon(i, 4);
            }
            this.listView1.Refresh();
        }

        private void restartAllClientsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (ClientProgram c in this.server.listClients)
            {
                c.sendQueue.Enqueue(new DataPackage(Command.Cmd, null, 1));
                c.sendQueue.Enqueue(CommandType.Restart);
            }
            foreach (ListViewItem i in this.listView1.Items)
            {
                this.ChangeIcon(i, 5);
            }
            this.listView1.Refresh();
        }

        private void hibernateAllCliToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (ClientProgram c in this.server.listClients)
            {
                c.sendQueue.Enqueue(new DataPackage(Command.Cmd, null, 1));
                c.sendQueue.Enqueue(CommandType.Hibernate);
            }
            foreach (ListViewItem i in this.listView1.Items)
            {
                this.ChangeIcon(i, 2);
            }
            this.listView1.Refresh();
        }

        private void lockAllClientsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (ClientProgram c in this.server.listClients)
            {
                c.sendQueue.Enqueue(new DataPackage(Command.Cmd, null, 1));
                c.sendQueue.Enqueue(CommandType.LockComputer);
            }
            foreach (ListViewItem i in this.listView1.Items)
            {
                this.ChangeIcon(i, 3);
            }
            this.listView1.Refresh();
        }
        #endregion

        #region -  CHANGE ICON WHEN TURN OFF CLIENT  -
        private void ChangeIcon(ListViewItem lvi, int id)
        {
            try
            {
                lvi.ImageIndex = id;
            }
            catch
            { }
        }
        #endregion

        #region -  ENABLE USB DETECTED  -
        private void enableUSBDetectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }
        #endregion

        #region -  VIEW LIST PROCESS  -
        private void viewListProcessesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ClientProgram c = this.server.FindClient(this.ipAddress);
                this.package = new DataPackage(Command.Cmd, null, 4);
                c.sendQueue.Enqueue(this.package);
                c.sendQueue.Enqueue(CommandType.GetListProcess);
            }
            catch
            { }
        }
        #endregion

        void attendenceCheckForm_OnSaveListStudentsToFile(object sender, SaveListStudentsToFileEventArgs e)
        {
            AttendenceCheck.WriteFile(e.Path, this.server.sb.ToString());
        }

        void attendenceCheckForm_OnActiveAttendenceCheck(object sender, ActiveAttendenceCheckEventArgs e)
        {
            this.package = new DataPackage(Command.Cmd, null, 4);
            foreach (ClientProgram c in this.server.listClients)
            {
                c.sendQueue.Enqueue(this.package);
                c.sendQueue.Enqueue(CommandType.AttendenceCheck);
            }
        }

        #region -  SET SAVE FILE TO DIRECTORY  -
        void saveFileToDirectory_OnSaveFileToDirectory(object sender, SaveFileToDirectoryEventArgs e)
        {
            AccessConfigFile.WriteFile(e.FilePath, "SaveFileToDirectory.conf");
            this.server.SetSaveFileToDirectory(e.FilePath);
        }
        #endregion

        #region -  FORM LOAD  -
        private void MainForm_Load(object sender, EventArgs e)
        {
            try
            {
                string[] listClients = AccessConfigFile.ReadFile("ListClients.dat").Split('|');
                foreach (string client in listClients)
                {
                    if (client.Trim().Length > 0)
                    {
                        this.listView1.Items.Add(client, 7);
                    }
                }
                this.attendenceCheckForm = new AttendenceCheckForm();
                this.attendenceCheckForm.OnActiveAttendenceCheck += new ActiveAttendenceCheckEventHandler(attendenceCheckForm_OnActiveAttendenceCheck);
                this.attendenceCheckForm.OnSaveListStudentsToFile += new SaveListStudentsToFileEventHandler(attendenceCheckForm_OnSaveListStudentsToFile);

                this.saveFileToDirectory = new SaveFileToDirectory();
                this.saveFileToDirectory.OnSaveFileToDirectory += new SaveFileToDirectoryEventHandler(saveFileToDirectory_OnSaveFileToDirectory);
            }
            catch
            { }
        }
        #endregion

        #region -  CLEAR LIST CLIENTS  -
        private void clearListClientsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.listView1.Items.Clear();
        }
        #endregion

        private void removeClientToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                this.listView1.Items.RemoveAt(this.id);
                this.listView1.Refresh();
            }
            catch
            {}
        }

        private void denyProgramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ClientProgram c = this.server.FindClient(this.ipAddress);
                c.ShowListDenyPro();
            }
            catch
            {
            }
        }

      

        

     private void remoteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void thoátChươngTrìnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void thôngTinTácGiảToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }
    }
}
